package com.yimixian.app.rest.api;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.model.KV;
import com.yimixian.app.model.SearchKeyResult;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by zhilian-2 on 2015/10/10.
 */
public class GetRedPointAPI extends YmxBaseAPI<List<KV>> {

    public static final String GETREDURL = "/v4/reddot/get";
    private String token;

    public GetRedPointAPI(String token, Callback<Response> cb){
        super(cb);
        this.token = token;
    }
    @Override
    public String getRelativeURL() {
        return GETREDURL;
    }

    public void getRedPoint(){
        if (!StringUtils.isEmpty(token)){
            mYmxRestService.getRedPoint(token, mCallback);
        }
    }
    @Override
    public List<KV> parseJson(JSONObject jsonObject) {
        if (jsonObject == null) return null;
        List<KV> kvs = new ArrayList<>();
        JSONArray array = jsonObject.optJSONArray("reddot");
        if (array != null){
            for (int i = 0; i < array.length(); i++){
                JSONObject object = array.optJSONObject(i);
                KV kv = new KV();
                kv.key = object.optString("key");
                kv.value = object.optString("count");
                kvs.add(kv);
            }
        }
        return kvs;
    }
}
